﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


            var Searchstudent = "select * from Students";
            var sk = DbHelper.Search(Searchstudent);
            dataGridView1.DataSource=sk;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect ;
            dataGridView1.ReadOnly=true;
            dataGridView1.AllowUserToDeleteRows =false;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            var name = textBox1.Text;
            var search = string.Format("select * from Students where Name like '%{0}%' ", name);
            var sk = DbHelper.Search(search);
            dataGridView1.DataSource = sk;
        }

        private void button2_Click(object sender, EventArgs e)
        {



            EditerForm editerform = new EditerForm();
            var res = editerform.ShowDialog();
            if (res == DialogResult.Yes)
            {

                var Searchstudent = "select * from Students";
                var sk = DbHelper.Search(Searchstudent);
                dataGridView1.DataSource = sk;
            }
            else
            {
                MessageBox.Show("对不起，添加失败", "信息框");
            }


        }

        private void button3_Click(object sender, EventArgs e)
        {
   
            var row = dataGridView1.SelectedRows.Count;
            if (row > 0)
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var Name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
                var Age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
                var Score = (int)dataGridView1.SelectedRows[0].Cells["Score"].Value;
                var Sex = (string)dataGridView1.SelectedRows[0].Cells["Sex"].Value;
                EditerForm editerform = new EditerForm(Id, Name, Age, Score, Sex);
                var res = editerform.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    var Searchstudent = "select * from Students";
                    var sk = DbHelper.Search(Searchstudent);
                    dataGridView1.DataSource = sk;
                }
                else
                {
                    MessageBox.Show("对不起，您未选择要更新的对象", "信息框");
                }
            }
            else
            {
                MessageBox.Show("对不起请你重新更新", "消息框");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
            var row = dataGridView1.SelectedRows.Count;
            if (row > 0)
            {
                var Id = dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var delete = string.Format("delete from Students where Id={0}", Id);
                var res = DbHelper.Addor(delete);
                if (res == 1)
                {
                    MessageBox.Show("删除成功", "消息框");

                    var Searchstudent = "select * from Students";
                    var sk = DbHelper.Search(Searchstudent);
                    dataGridView1.DataSource = sk;

                }
            


                else
                {
                    MessageBox.Show("删除失败", "消息框");
                }
            }
            else
            {
                MessageBox.Show("对不起为选择删除对象", "消息框");
            }
    

        }

        }

       

        }
    

